package com.example.lenovo.test1;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

import java.util.ArrayList;

public class Main10Activity extends AppCompatActivity  {

    private ExpandableListView myExpandableListView;
    private  ArrayList<ExpandableListViewItemData> mArrayListExpandableListViewGroupItemData;

    private void addExpandableListViewChildItemData(ExpandableListViewItemData expandableListViewItemData, int nSubID) {
        switch (nSubID) {
            case 1:
                expandableListViewItemData.arrayListExpandableListViewChildItemData.add(new ExpandableListViewItemData.ExpandableListViewChildItemData("芳村站"));
                expandableListViewItemData.arrayListExpandableListViewChildItemData.add(new ExpandableListViewItemData.ExpandableListViewChildItemData("长寿路站"));
                expandableListViewItemData.arrayListExpandableListViewChildItemData.add(new ExpandableListViewItemData.ExpandableListViewChildItemData("公园前站"));
                expandableListViewItemData.arrayListExpandableListViewChildItemData.add(new ExpandableListViewItemData.ExpandableListViewChildItemData("农讲所站"));
                break;
            case 2:
                expandableListViewItemData.arrayListExpandableListViewChildItemData.add(new ExpandableListViewItemData.ExpandableListViewChildItemData("公园前站"));
                expandableListViewItemData.arrayListExpandableListViewChildItemData.add(new ExpandableListViewItemData.ExpandableListViewChildItemData("海珠广场站"));
                expandableListViewItemData.arrayListExpandableListViewChildItemData.add(new ExpandableListViewItemData.ExpandableListViewChildItemData("江南西站"));
                expandableListViewItemData.arrayListExpandableListViewChildItemData.add(new ExpandableListViewItemData.ExpandableListViewChildItemData("江泰路站"));
                break;
            case 3:
                expandableListViewItemData.arrayListExpandableListViewChildItemData.add(new ExpandableListViewItemData.ExpandableListViewChildItemData("同和站"));
                expandableListViewItemData.arrayListExpandableListViewChildItemData.add(new ExpandableListViewItemData.ExpandableListViewChildItemData("永泰站"));
                expandableListViewItemData.arrayListExpandableListViewChildItemData.add(new ExpandableListViewItemData.ExpandableListViewChildItemData("龙归站"));
                break;
            default:
                break;
        }
    }
    private ExpandableListViewItemData createExpandableListViewItemData(String title, int nSubID) {
        ExpandableListViewItemData expandableListViewItemData = new ExpandableListViewItemData();
        expandableListViewItemData.expandableListViewGroupItemData.groupItemTitle = title;
        addExpandableListViewChildItemData(expandableListViewItemData,nSubID);
        return expandableListViewItemData;
    }
    protected void initData() {
        mArrayListExpandableListViewGroupItemData = new ArrayList<ExpandableListViewItemData>();
        mArrayListExpandableListViewGroupItemData.add(createExpandableListViewItemData("1号线", 1));
        mArrayListExpandableListViewGroupItemData.add(createExpandableListViewItemData("2号线", 2));
        mArrayListExpandableListViewGroupItemData.add(createExpandableListViewItemData("3号线", 3));
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main10);
        myExpandableListView = findViewById(R.id.myExpandableListView);
        initData();

        MyExpandableAdapter myExpandableAdapter = new MyExpandableAdapter();
        myExpandableListView.setAdapter(myExpandableAdapter);
    }

    private  class MyExpandableAdapter extends BaseExpandableListAdapter {
        @Override
        public int getGroupCount() {
            return mArrayListExpandableListViewGroupItemData.size();
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            ExpandableListViewItemData expandableListViewItemData =
                    mArrayListExpandableListViewGroupItemData.get(groupPosition);
            return expandableListViewItemData.arrayListExpandableListViewChildItemData.size();
        }

        @Override
        public Object getGroup(int groupPosition) {
            return mArrayListExpandableListViewGroupItemData.get(groupPosition);
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            ExpandableListViewItemData expandableListViewItemData =
                    mArrayListExpandableListViewGroupItemData.get(groupPosition);
            return expandableListViewItemData.arrayListExpandableListViewChildItemData.get(childPosition);
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            GroupViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(Main10Activity.this, R.layout.group_date, null);
                holder = new GroupViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (GroupViewHolder) convertView.getTag();
            }
            holder.tv_group.setText(mArrayListExpandableListViewGroupItemData.get(groupPosition).expandableListViewGroupItemData.groupItemTitle);
            return convertView;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            ChildViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(Main10Activity.this, R.layout.child_date, null);
                holder = new ChildViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ChildViewHolder) convertView.getTag();
            }
            holder.tv_child.setText(mArrayListExpandableListViewGroupItemData.get(groupPosition).arrayListExpandableListViewChildItemData.get(childPosition).childItemTitle);
            return convertView;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

        public class GroupViewHolder {
            TextView tv_group;
            public GroupViewHolder(View convertView) {
                tv_group = (TextView) convertView.findViewById(R.id.tv_group);
            }
        }

        public class ChildViewHolder {
            TextView tv_child;

            public ChildViewHolder(View convertView) {
                tv_child = (TextView) convertView.findViewById(R.id.tv_child);
            }
        }
    }




}
